package org.nf.shopping.service.Impl;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.nf.shopping.common.exception.ErrorsEnum;
import org.nf.shopping.common.exception.LoginException;
import org.nf.shopping.entity.User;
import org.nf.shopping.entity.UserInfo;
import org.nf.shopping.mapper.UserInfoMapper;
import org.nf.shopping.service.UserInfoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author czl
 * @Date 2024-12-25
 */
@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl implements UserInfoService {

    private final UserInfoMapper userInfoMapper;

    @Override
    public PageInfo<UserInfo> userInfoPage(Integer pageSize, Integer pageNum, String name) {
        List<UserInfo> list = userInfoMapper.listUserInfo(pageSize,pageNum,name);
        PageInfo<UserInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void updateUserInfo(UserInfo userInfo) {
        userInfoMapper.updateUserInfo(userInfo);
    }

    @Override
    public void deloneUserInfo(Integer id) {
        userInfoMapper.deloneUserInfo(id);
    }

    @Override
    public void addUserInfo(UserInfo userInfo) {
        userInfoMapper.addUserInfo(userInfo);
    }

    @Override
    public List<UserInfo> getUserInfoList() {
        return userInfoMapper.getUserInfoList();
    }

    @Override
    public UserInfo auto(String userInfoName, String userInfoPwd) {
        UserInfo userInfo = userInfoMapper.getUserInfo(userInfoName);
        if (userInfo != null){
            if (userInfoPwd.equals(userInfo.getUserInfoPwd())){
                return userInfo;
            }
        }
        throw new LoginException(ErrorsEnum.LOGIN_EXCEPTION);
    }

    @Override
    public int getAddUserInfo(String userInfoName, String userInfoPwd) {
        if ((userInfoMapper.getUserInfoName(userInfoName)== null)){
            UserInfo userInfo = new UserInfo(null,userInfoName,userInfoPwd,null);
            return userInfoMapper.addUserInfoId(userInfo);
        }
        throw new LoginException(ErrorsEnum.USER_ALREADY_EXISTS);
    }

}
